package com.atguigu.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.edu.entity.EduSubject;
import com.atguigu.edu.entity.excel.SubjectData;
import com.atguigu.edu.entity.excel.SubjectReaderListener;
import com.atguigu.edu.mapper.EduSubjectMapper;
import com.atguigu.edu.service.EduSubjectService;
import com.atguigu.edu.entity.vo.tree.EduSubjectNode;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author alexzshl
 * @since 2021-05-04
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubjectFromFile(MultipartFile file) {
        InputStream is = null;
        SubjectReaderListener subjectReaderListener = new SubjectReaderListener(this);
        try {
            is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class, subjectReaderListener).sheet("subjects").doRead();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        List<SubjectData> list = subjectReaderListener.getList();
        list.forEach(System.out::println);
    }

    @Override
    public List<EduSubjectNode> getSubjectTree() {
        List<EduSubject> eduSubjects = this.list(null);
        ArrayList<EduSubjectNode> rootEduSubjectNodeList = new ArrayList<>();

        // 先创建节点映射
        HashMap<String, EduSubjectNode> map = new HashMap<>();
        for (EduSubject eduSubject : eduSubjects) {
            String id = eduSubject.getId();

            // String title = eduSubject.getTitle();
            // String pid = eduSubject.getParentId();
            // EduSubjectNode eduSubjectNode = new EduSubjectNode();
            // eduSubjectNode.setId(id).setTitle(title).setPid(pid).setChildren(new ArrayList<EduSubjectNode>());

            // EduSubjectNode eduSubjectNode = new EduSubjectNode();
            // BeanUtils.copyProperties(eduSubject, eduSubjectNode);

            String title = eduSubject.getTitle();
            String pid = eduSubject.getParentId();
            EduSubjectNode eduSubjectNode = new EduSubjectNode();
            eduSubjectNode.setId(id).setValue(id).setTitle(title).setPid(pid);

            map.put(id, eduSubjectNode);
        }

        System.out.println(map.size());

        // 重新分析节点创建关系树
        for (EduSubject eduSubject : eduSubjects) {
            String id = eduSubject.getId();
            String pid = eduSubject.getParentId();
            EduSubjectNode eduSubjectNode = map.get(id);
            EduSubjectNode parentEduSubjectNode = map.get(pid);

            // 将一级节点加入根节点树对象
            if ("0".equals(pid)) {
                rootEduSubjectNodeList.add(eduSubjectNode);
            } else {
                // 为父节点创建Children列表
                if (parentEduSubjectNode.getChildren() == null) {
                    parentEduSubjectNode.setChildren(new ArrayList<>());
                }
                // 非一级节点加入其父节点children
                parentEduSubjectNode.getChildren().add(eduSubjectNode);
            }
        }

        return rootEduSubjectNodeList;
    }


}
